using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sample
{
    public partial class CustomerForm : System.Web.UI.Page
    {
        public enum DisplayType
        {
            Edit, Add
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if(GetDisplayType == DisplayType.Add)
                {
                    using (DataClasses1DataContext db = new DataClasses1DataContext())
                    {
                    
                        FillCustomerType(db);
                    
                    }
                }            
                if (GetDisplayType == DisplayType.Edit)
                {
                    using (DataClasses1DataContext db = new DataClasses1DataContext())
                    {
                        Customer entity = db.Customers.Single(_ => _.Id == GetEditID);
                        CustomerCustomerNameInput.Text = entity.CustomerName;
                        FillCustomerType(db);
          CustomerTypeIdInput.SelectedValue = entity.CustomerType.ToString()
        ;
                        CustomerCustomerDescriptionInput.Text = entity.CustomerDescription;
                        }
                }
            }
        }
        
        
        
          private void FillCustomerType(DataClasses1DataContext db)
          {
              CustomerTypeIdInput.DataSource = db.CustomerTypes;
              CustomerTypeIdInput.DataTextField = "CustomerTypeName";
              CustomerTypeIdInput.DataValueField = "Id";
              CustomerTypeIdInput.DataBind();
          }
        
        
        public DisplayType GetDisplayType
        {
            get
            {
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                    return DisplayType.Edit;
                else
                    return DisplayType.Add;
            }
        }
        private int GetEditID
        {
            get { return int.Parse(Request.QueryString["ID"]); }
        }
        protected void CustomerSaveButton_Click(object sender, EventArgs e)
        {
            using (DataClasses1DataContext db = new DataClasses1DataContext())
            {
                Customer entity;
                if (GetDisplayType == DisplayType.Edit)
                    entity = db.Customers.Single(_ => _.Id == GetEditID);
                else
                {
                    entity = new Customer();
                    db.Customers.InsertOnSubmit(entity);
                }
                entity.CustomerName = CustomerCustomerNameInput.Text;
                entity.CustomerType = int.Parse(CustomerTypeIdInput.SelectedValue);
                entity.CustomerDescription = CustomerCustomerDescriptionInput.Text;
                db.SubmitChanges();
            }
        }
        protected void CustomerCancelButton_Click(object sender, EventArgs e)
        {

        }
    }
}